package com.lzw.breezeNovel.webApp.service.impl.novel;


import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.lzw.breezeNovel.webApp.config.DashScopeConfig;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;
/**
 * @author 清风明月
 * @Package com.lzw.breezeNovel.webApp.service
 * @date 2025/3/6
 * @description 客服服务类
 */
@Service
@SuppressWarnings("all")
public class CustomerServiceimpl {
    /**
     * 注入配置类
     */
    @Autowired
    private DashScopeConfig dashScopeConfig;

    /**
     * 创建生成参数对象。
     *
     * @param messages 消息列表
     * @return 生成参数对象
     */
    public GenerationParam createGenerationParam(List<Message> messages) {
        return GenerationParam.builder()
                .model(dashScopeConfig.getModel())
                .messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .apiKey(dashScopeConfig.getApiKey())
                .topP(0.8)
                .build();
    }

    /**
     * 调用带有消息的生成服务。
     *
     * @param param 生成参数
     * @return 生成结果
     * @throws ApiException      API 异常
     * @throws NoApiKeyException 缺少 API key 异常
     * @throws InputRequiredException 输入必需异常
     */
    public GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        return gen.call(param);
    }

    /**
     * 创建消息对象。
     *
     * @param role    角色
     * @param content 内容
     * @return 消息对象
     */
    public Message createMessage(@NotNull Role role, String content) {
        return Message.builder().role(role.getValue()).content(content).build();
    }
}
